package org.example.service;

import org.example.domain.Book;
import org.example.domain.BookExample;
import org.example.domain.BorrowRecord;
import org.example.mapper.dao.BookDao;
import org.example.mapper.dao.BorrowRecordDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BookService {
    public final static String PRE_BORROW="0";
    public final static String BORROW="1";
    public final static String PRE_RETURN="10";
    public final static String RETURN="20";
    @Resource
    BookDao bookDao;

    @Resource
    BorrowRecordDao borrowRecordDao;


    public List<Book> getBooks(){
        BookExample e=new BookExample();
        return bookDao.selectByExampleWithBLOBs(e);
    }

    public List<Book> searchBook(String str){
        return bookDao.searchBook(str);
    }

    public Book getBook(String isbn){
        return bookDao.selectByPrimaryKey(isbn);
    }

    public boolean preBoroow(int userId,String isbn){
        BorrowRecord curRecord =borrowRecordDao.getCurRecord(isbn);
        if(curRecord!=null){
            return false;
        }else {
            //todo 校验数据是否存在

            BorrowRecord record = new BorrowRecord();
//            record.setId(curRecord.getId());
            record.setIsbn(isbn);
            record.setUserId(userId);
            record.setBorrowState(PRE_BORROW);
            record.setPreBorrowTime(new Date());
            borrowRecordDao.insertSelective(record);
        }
        return true;
    }

    public boolean boroow(int userId,int id){
//        BorrowRecord curRecord =borrowRecordDao.getCurRecord(isbn);
        BorrowRecord curRecord =borrowRecordDao.selectByPrimaryKey(id);
        if(curRecord!=null){
            BorrowRecord record = new BorrowRecord();
            record.setId(curRecord.getId());
//            record.setUserId(userId);
            record.setBorrowManagerId(userId);
            record.setBorrowState(BORROW);
            record.setBorrowTime(new Date());
            borrowRecordDao.updateByPrimaryKey(record);

        }else {
            return false;

        }
        return true;
    }

    public boolean preBookReturn(int userId,int id){
        BorrowRecord curRecord =borrowRecordDao.selectByPrimaryKey(id);
        if(curRecord!=null){
            //todo  用户校验  状态校验
            BorrowRecord record = new BorrowRecord();
            record.setId(curRecord.getId());
//            record.setUserId(userId);
//            record.setBorrowManagerId(userId);
            record.setBorrowState(PRE_RETURN);
            record.setPreReturnTime(new Date());
            borrowRecordDao.updateByPrimaryKey(record);
        }else {



            return false;
        }
        return true;
    }

    public boolean bookReturn(int userId,int id){
        BorrowRecord curRecord =borrowRecordDao.selectByPrimaryKey(id);
        if(curRecord!=null){
            BorrowRecord record = new BorrowRecord();
            record.setId(curRecord.getId());
//            record.setUserId(userId);
//            record.setBorrowManagerId(userId);
            record.setBorrowState(RETURN);
            record.setReturnTime(new Date());
            borrowRecordDao.updateByPrimaryKey(record);

        }else {
            return false;

        }
        return true;
    }
}
